package com.vgooo.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


import android.app.AlertDialog;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

/**
 * 转换时间的工具
 * 
 * @author Administrator
 * 
 */
public class TimeUtils {
	
	
	public static final long		MINUTE_MILLIS 					= 60 * 1000; 
	public static final long		HOUR_MILLIS 					= MINUTE_MILLIS * 60; 
	/**
	 * milliseconds of a day
	 */
	public static final long 		DAY_MILLIS 						= HOUR_MILLIS * 24;
	/**
	 * milliseconds of a half day
	 */
	public static final long 		HALF_DAY_MILLIS 				= DAY_MILLIS / 2;
	/**
	 * milliseconds of a week
	 */
	public static final long 		WEEK_MILLIS 					= DAY_MILLIS * 7;
	/**
	 * milliseconds of a month
	 */
	public static final long 		MONTH_MILLIS 					= WEEK_MILLIS * 30;
	public static final long 		HALF_MONTH_MILLIS 				= MONTH_MILLIS / 2;
	/**
	 * yyyyMMdd
	 */
	public static final String 		DATE_DEFAULT_FORMATE 			= "yyyyMMdd";
	/**
	 * yyyy-MM-dd HH:mm:ss   2010-05-11 17:22:26
	 */
	public static final String 		DATE_FORMATE_ALL 				= "yyyy-MM-dd HH:mm:ss";
	/**
	 * dd/MM/yyyy, hh:mm
	 */
	public static final String 		DATE_FORMATE_TRANSACTION		= "dd/MM/yyyy, hh:mm";
	/**
	 * MM/dd HH:mm
	 */
	public static final String 		DATE_FORMATE_DAY_HOUR_MINUTE 	= "MM/dd HH:mm";
	/**
	 * HH:mm
	 */
	public static final String 		DATE_FORMATE_HOUR_MINUTE 		= "HH:mm";
	public static final String 		DATE_FORMATE_HOUR_MINUTE_SECOND 		= "HH:mm:ss";
	
	public static SimpleDateFormat	 dateFormate =new SimpleDateFormat();
	/**
	 * 
	 * @param milliseconds 
	 * @return the time formated by "yyyy-MM-dd HH:mm:ss"
	 */
	public static String toTime(long milliseconds){
		return toTime(new Date(milliseconds),DATE_FORMATE_ALL);
	}
	/**
	 * 
	 * @param milliseconds
	 * @param pattern
	 * @return the time formated
	 */
	public static String toTime(long milliseconds,String pattern){
		return toTime(new Date(milliseconds),pattern);
	}
	/**
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String toTime(Date date,String pattern){
		if(TextUtils.isEmpty(pattern))
		{
			pattern = DATE_DEFAULT_FORMATE;
		}
		dateFormate.applyPattern(pattern);
		if(date ==null)
		{
			date = new Date();
		}
		try
		{
			return dateFormate.format(date);
		}catch (Exception e) {
			return "";
		}
	}
	
	
	/**
	 * 获取目前的系统时间
	 * 
	 * @return long java格式系统时间
	 */
	public static long getSystemtTimeJ() {
		return System.currentTimeMillis();
	}

	/**
	 * 获取目前的系统时间
	 * 
	 * @return long Php格式系统时间
	 */
	public static String getSystemtTimeP() {
		return System.currentTimeMillis() / 1000 + "";
	}

	/**
	 * 格式化时间
	 * 
	 * @param time
	 *            long java格式的时间
	 * @return time String
	 */
	public static String formatTime(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		return sdf.format(new Date(time));
	}
	/**
	 * 格式化时间
	 * 
	 * @param time
	 *            String Php格式的时间
	 * @return time String
	 */
	public static String formatTime(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		return sdf.format(new Date(p2JTime(time)));
	}

	/**
	 * 将Php格式的事件转换为java格式的时间
	 * 
	 * @param time
	 *            String Php
	 * @return time long Java格式时间
	 */
	public static long p2JTime(String time) {
		return Long.parseLong(time + "000");
	}

	/**
	 * 将java格式的时间转换为Php格式的时间
	 * 
	 * @param time
	 *            long java
	 * @return time String Php格式时间
	 */
	public static String j2PTime(long time) {
		return "" + time / 1000;
	}

	/**
	 * 获取分钟
	 * 
	 * @param time
	 *            long java格式的时间
	 * @return minutes int
	 */
	public static int getMinutes(long time) {
		return new Date(time).getMinutes();
	}

	/**
	 * 获取分钟
	 * 
	 * @param time
	 *            String Php格式的时间
	 * @return minutes int
	 */
	public static int getMinutes(String time) {
		return getMinutes(p2JTime(time + "000"));
	}

	/**
	 * 获取小时
	 * 
	 * @param time
	 *            long java格式的时间
	 * @return hours int
	 */
	public static int getHours(long time) {
		return new Date(time).getHours();
	}

	/**
	 * 获取小时
	 * 
	 * @param time
	 *            String Php格式的时间
	 * @return hours int
	 */
	public static int getHours(String time) {
		return getHours(p2JTime(time + "000"));
	}

	/**
	 * 获取天数
	 * 
	 * @param time
	 *            long java格式的时间
	 * @return date int
	 */
	public static int getDate(long time) {
		return new Date(time).getDate();
	}

	/**
	 * 获取天数
	 * 
	 * @param time
	 *            String Php格式的时间
	 * @return date int
	 */
	public static int getDate(String time) {
		return getDate(p2JTime(time + "000"));
	}

	/**
	 * 
	 * @param time
	 *            long java格式的时间
	 * @return month int
	 */
	public static int getMonth(long time) {
		return new Date(time).getMonth();
	}

	/**
	 * 
	 * @param time
	 *            String java格式的时间
	 * @return month int
	 */
	public static int getMonth(String time) {
		return getMonth(p2JTime(time + "000"));
	}

	/**
	 * 
	 * @param time
	 *            long java格式的时间
	 * @return year int
	 */
	public static int getYear(long time) {
		return new Date(time).getYear();
	}

	/**
	 * 
	 * @param time
	 *            String Php格式的时间
	 * @return year int
	 */
	public static int getYear(String time) {
		return getYear(p2JTime(time + "000"));
	}

	/**
	 * 和现在间隔多久
	 * 
	 * @param time Php格式的时间
	 * @return 1分钟（小时、月、年）前
	 */
	public static String rangeNow(String time) {
		return rangeNow(p2JTime(time));

	}
	/**
	 * 和现在间隔多久
	 * 
	 * @param time java格式的时间
	 * @return 1分钟（小时、月、年）前
	 */
	public static String rangeNow(long time) {
		long now = getSystemtTimeJ();
		int year, month, day, hour, minute;
		if ((year = getYear(now) - getYear(time)) > 0) {
			log("year = " + year);
			return year + "年前";
		} else if ((month = getMonth(now) - getMonth(time)) > 0) {
			log("month = " + month);
			return month + "月前";
		} else if ((day = getDate(now) - getDate(time)) > 0) {
			log("day = " + day);
			return day + "天前";
		} else if ((hour = getHours(now) - getHours(time)) > 0) {
			log("hour = " + hour);
			return hour + "小时前";
		} else if ((minute = getMinutes(now) - getMinutes(time)) > 0) {
			log("minute = " + minute);
			return minute + "分钟前";
		} else {
			return "1分钟以内";
		}
		
	}
	/**
	 * 延时时间取消显示的对话框
	 * @param dialog 对话框
	 * @param time 延时的时间
	 */
	public static void TimeTask(AlertDialog dialog , long time){
		TimerTask task = new MyTimeTash(dialog);
		Timer timer = new Timer() ;
		timer.schedule(task, new Date(TimeUtils.getSystemtTimeJ()+time));
	}
	
	static class MyTimeTash extends TimerTask{

		AlertDialog dialog ;
		
		public MyTimeTash(AlertDialog dialog) {
			super();
			this.dialog = dialog;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			dialog.cancel() ;
		}
		
		
	}
	
	/**
	 * 控制小圆点循环滚动
	 * @author Administrator
	 *
	 */
	public static class Looping {
		
		private Handler hander ;
		private boolean isRun ;
		private Thread thread  ;
		private CallBack callBack = new CallBack() {
			
			@Override
			public void next() {
			}
		};
		
		
		public Looping() {
			super();
			this.hander = new Handler(){
				public void handleMessage(android.os.Message msg) {
					callBack.next();
				};
			} ;
		}
		
		public void start(final int time){
			isRun = true ;
			thread = new Thread(){
				public void run() {
					try {
						while (isRun) {
							this.sleep(time) ;
							hander.sendEmptyMessage(time);
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				};
			} ;
			thread.start();
		}
		
		public void stop(){
			isRun = false ;
		}
		
		public void setCallBack(CallBack callBack) {
			this.callBack = callBack;
		}


		public interface CallBack{
			 void next();
		};
		
		
	}
	
	
	/**
	 * 打印日志，调试信息
	 * 
	 * @param info
	 */
	public static void log(String info) {
		DebugTools.log("TimeUtils", info);
//		Log.i("TimeUtils", info);
	}

}
